package com.example.boxuegu;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.Selection;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

public class chage_user extends AppCompatActivity {

    private String title;
    private String content;
    private TextView tv_main_title;
    private int flag;//flag为1时表示修改昵称，2为修改签名
    private RelativeLayout title_bar;
    private TextView tv_back;
    private TextView tv_save;
    private EditText et_content;
    private ImageView iv_del;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chage_user);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//设置为竖屏
        init();

    }
    //获取界面控件
    private void init() {
        title = getIntent().getStringExtra("title");
        content = getIntent().getStringExtra("content");
        flag = getIntent().getIntExtra("flag", 0);
        tv_main_title = findViewById(R.id.tv_main_title);
        tv_main_title.setText(title);
        title_bar = (RelativeLayout) findViewById(R.id.title_bar);
        title_bar.setBackgroundColor(Color.parseColor("#30B4FF"));
        tv_back = (TextView) findViewById(R.id.tv_back);
        tv_save = (TextView) findViewById(R.id.tv_save);
        tv_save.setVisibility(View.VISIBLE);
        et_content = findViewById(R.id.et_content);
        iv_del = findViewById(R.id.iv_del);
        if(!TextUtils.isEmpty(content)){
            et_content.setText(content);
            et_content.setSelection(content.length());

        }
        contentListener();
        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chage_user.this.finish();
            }
        });
        iv_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et_content.setText("");
            }
        });
        tv_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent data = new Intent();
                String etContent = et_content.getText().toString().trim();
                switch (flag){
                    case 1:
                        if(!TextUtils.isEmpty(etContent)){
                            data.putExtra("nickName",etContent);
                            setResult(RESULT_OK,data);
                            Toast.makeText(chage_user.this,"保存成功",Toast.LENGTH_SHORT).show();
                            chage_user.this.finish();
                        }else {
                            Toast.makeText(chage_user.this,"昵称不能为空",Toast.LENGTH_SHORT).show();
                        }break;
                    case 2:
                        if(!TextUtils.isEmpty(etContent)){
                            data.putExtra("signature",etContent);
                            setResult(RESULT_OK,data);
                            Toast.makeText(chage_user.this,"保存成功",Toast.LENGTH_SHORT).show();
                            chage_user.this.finish();
                        }else{
                            Toast.makeText(chage_user.this,"签名不能为空",Toast.LENGTH_SHORT).show();
                        }break;
                }
            }
        });
    }
    //监听个人资料修界面输入的文字
    private void contentListener() {
        et_content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Editable editable = et_content.getText();
                int length = editable.length();//输入文本的长度
                if(length>0){
                    iv_del.setVisibility(View.VISIBLE);
                }else{
                    iv_del.setVisibility(View.GONE);
                }
                switch(flag){
                    case 1://昵称
                        if(length>8){
                            int selectionEnd = Selection.getSelectionEnd(editable);
                            String s1 = editable.toString();
                            String newStr = s1.substring(0, 8);//截取新字符串
                            et_content.setText(newStr);
                            editable = et_content.getText();//新字符的长度
                            int newLen = editable.length();//旧光标位置超过新字符串的长度
                            if(selectionEnd>newLen){
                                selectionEnd=editable.length();
                            }
                            //设置新光标的位置
                            Selection.setSelection(editable,selectionEnd);
                        }
                        break;
                    case 2://签名
                        /*
                        * 签名最多是16个文字，超过16个需要截取掉多余的文字*/
                        if (length>16){
                            int selectionEnd = Selection.getSelectionEnd(editable);
                            String s1 = editable.toString();
                            //截取新的字符串
                            String newStr = s1.substring(0, 16);
                            et_content.setText(newStr);
                            editable= et_content.getText();
                            //新字符串长度
                            int newLen = editable.length();//旧光标位置超出字符串的长度
                            if(selectionEnd>newLen){
                                selectionEnd=editable.length();

                            }
                            //设置光标所在位置
                            Selection.setSelection(editable,selectionEnd);
                        }
                        break;
                    default:
                        break;
                }

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
}